<?php
include_once './config/config.php';

$where = 1;

$content = $_GET['content'];

if (!empty($content)) {
    $where = "`username` LIKE '%$content%'";
}

// 获取当前的页码
$page = isset($_GET['page']) ? $_GET['page'] : 1;

// 获取person表的数据总数
$CountSql = "SELECT COUNT(*) AS num FROM `pre_admin` WHERE '%$where%'";
$count = query($CountSql);

// 每页显示多少条数据
$limit = 5;

// 每页显示多少个分页的页码数
$size = 5;

$ShowPage = page($page, $count['num'], $limit, $size);

// 偏移量 -> 索引值
// ? $start = ($page - 1); 有时候偏移量需要 乘上 $limit
$start = ($page - 1) * $limit;

$sql = "SELECT * FROM `pre_admin` WHERE $where LIMIT $start,$limit";
$dataList = queryAll($sql);

if ($_POST) {
    $sql = "SELECT * FROM `pre_admin` WHERE `id` = '{$_POST['id']}'";
    $data = query($sql);

    // $pre($data['avatar']);
    // exit;
    @delfile($data['avatar']);

    $where = "`id` = '{$_POST['id']}'";
    deleteById("admin", $where);
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('common/meta.php'); ?>
</head>
<style>
    td {
        vertical-align: middle !important;
    }

    .pagination {
        text-align: center;
    }

    .btn-toolbar {
        display: flex;
        align-items: center;
    }

    #search_form {
        display: flex;
        align-items: center;
        margin: 0;
        margin-left: 20px;
    }
</style>

<body>
    <!--<![endif]-->

    <?php include_once('common/header.php'); ?>

    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">管理员列表</h1>
        </div>
        <ul class="breadcrumb">
            <li>AdminList <span class="divider">/</span></li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='adminAdd.php'"><i class="icon-plus"></i>添加管理员</button>

                    <?php include_once 'common/form.php'; ?>
                </div>
                <div class="well">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>头像</th>
                                <th>用户名</th>
                                <th>权限</th>
                                <th>密码</th>
                                <th>密码盐</th>
                                <th style="width: 30px;">
                                    操作
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($dataList as $item) { ?>
                                <tr>
                                    <td>
                                        <?php echo $item['id']; ?>
                                    </td>
                                    <!-- avatar -->
                                    <td>
                                        <img src="<?php echo './upload/' . $item['avatar']; ?>" alt='' width="60">
                                    </td>
                                    <td>
                                        <?php echo $item['username']; ?>
                                    </td>
                                    <td>
                                        <?php echo $item['role'] === 1 ? '超级管理员':'普通管理员' ?>
                                    </td>
                                    <td>
                                        <?php echo $item['password']; ?>
                                    </td>
                                    <td>
                                        <?php echo $item['salt']; ?>
                                    </td>
                                    <td>
                                        <?php echo "<a href='adminUpdata.php?adminid={$item['id']}'><i class='icon-pencil'></i></a>" ?>

                                        <?php echo "<a href='#myModal'  role='button' data-id='{$item['id']}' data-toggle='modal'  class='sureBtn'><i class='icon-remove'></i></a>" ?>
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>
                <div class="pagination">
                    <?php echo $ShowPage; ?>
                </div>

                <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h3 id="myModalLabel">警告</h3>
                    </div>
                    <div class="modal-body">
                        <p class="error-text"><i class="icon-warning-sign modal-icon"></i>确定删除？</p>
                    </div>
                    <div class="modal-footer">
                        <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                        <button class="btn btn-danger deleteBtn" data-dismiss="modal">删除</button>
                    </div>
                </div>

                <?php include_once('common/footer.php'); ?>

                <script src="./assets/lib/jquery-1.8.1.min.js"></script>
                <script>
                    $('.sureBtn').on('click', function() {
                        $('.deleteBtn').attr("data-id", $(this).attr('data-id'))
                    });

                    $('.deleteBtn').on('click', function() {
                        // console.log($(this).attr('data-id'));
                        var delId = $(this).attr('data-id');
                        $.ajax({
                            type: "post",
                            url: "adminList.php",
                            data: {
                                id: delId
                            },
                            dataType: "json",
                            success: function(res) {
                                alert(res.msg);
                                location.reload();
                            },
                            error: function(error) { // 错误回调的函数
                                console.log(error);
                            }
                        });
                    })
                </script>
            </div>
        </div>
    </div>
</body>

</html>
<?php include_once('common/script.php'); ?>